package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.OperatingHours;

@FacesConverter(value = "operatingHoursConverter", forClass = util.enumeration.OperatingHours.class)
public class OperatingHoursConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        if (value == null || value.length() == 0) {
            return null;
        }

        if(value.equals("EightAM")) return OperatingHours.EightAM;
        else if(value.equals("EightPM")) return OperatingHours.EightPM;
        else if(value.equals("ElevenAM")) return OperatingHours.ElevenAM;
        else if(value.equals("ElevenPM")) return OperatingHours.ElevenPM;
        else if(value.equals("FiveAM")) return OperatingHours.FiveAM;
        else if(value.equals("FivePM")) return OperatingHours.FivePM;
        else if(value.equals("FourAM")) return OperatingHours.FourAM;
        else if(value.equals("FourPM")) return OperatingHours.FourPM;
        else if(value.equals("NineAM")) return OperatingHours.NineAM;        
        else if(value.equals("NinePM")) return OperatingHours.NinePM;        
        else if(value.equals("OFF")) return OperatingHours.OFF;
        else if(value.equals("OneAM")) return OperatingHours.OneAM;
        else if(value.equals("OnePM")) return OperatingHours.OnePM;
        else if(value.equals("SevenAM")) return OperatingHours.SevenAM;        
        else if(value.equals("SevenPM")) return OperatingHours.SevenPM;        
        else if(value.equals("SixAM")) return OperatingHours.SixAM;        
        else if(value.equals("SixPM")) return OperatingHours.SixPM;        
        else if(value.equals("TenAM")) return OperatingHours.TenAM;        
        else if(value.equals("TenPM")) return OperatingHours.TenPM;        
        else if(value.equals("ThreeAM")) return OperatingHours.ThreeAM;        
        else if(value.equals("ThreePM")) return OperatingHours.ThreePM;        
        else if(value.equals("TwelveAM")) return OperatingHours.TwelveAM;        
        else if(value.equals("TwelvePM")) return OperatingHours.TwelvePM;        
        else if(value.equals("TwoAM")) return OperatingHours.TwoAM;        
        else if(value.equals("TwoPM")) return OperatingHours.TwoPM;        
        else return OperatingHours.OFF;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof OperatingHours) {
            OperatingHours operatingHours = (OperatingHours) value;

            if(operatingHours.equals(OperatingHours.EightAM)) return "EightAM";
            else if(operatingHours.equals(OperatingHours.EightPM)) return "EightPM";
            else if(operatingHours.equals(OperatingHours.ElevenAM)) return "ElevenAM";
            else if(operatingHours.equals(OperatingHours.ElevenPM)) return "ElevenPM";
            else if(operatingHours.equals(OperatingHours.FiveAM)) return "FiveAM";
            else if(operatingHours.equals(OperatingHours.FivePM)) return "FivePM";
            else if(operatingHours.equals(OperatingHours.FourAM)) return "FourAM";
            else if(operatingHours.equals(OperatingHours.FourPM)) return "FourPM";
            else if(operatingHours.equals(OperatingHours.NineAM)) return "NineAM";
            else if(operatingHours.equals(OperatingHours.NinePM)) return "NinePM";
            else if(operatingHours.equals(OperatingHours.OFF)) return "OFF";
            else if(operatingHours.equals(OperatingHours.OneAM)) return "OneAM";
            else if(operatingHours.equals(OperatingHours.OnePM)) return "OnePM";
            else if(operatingHours.equals(OperatingHours.SevenAM)) return "SevenAM";
            else if(operatingHours.equals(OperatingHours.SevenPM)) return "SevenPM";
            else if(operatingHours.equals(OperatingHours.SixAM)) return "SixAM";
            else if(operatingHours.equals(OperatingHours.SixPM)) return "SixPM";
            else if(operatingHours.equals(OperatingHours.TenAM)) return "TenAM";
            else if(operatingHours.equals(OperatingHours.TenPM)) return "TenPM";
            else if(operatingHours.equals(OperatingHours.ThreeAM)) return "ThreeAM";
            else if(operatingHours.equals(OperatingHours.ThreePM)) return "ThreePM";
            else if(operatingHours.equals(OperatingHours.TwelveAM)) return "TwelveAM";
            else if(operatingHours.equals(OperatingHours.TwelvePM)) return "TwelvePM";
            else if(operatingHours.equals(OperatingHours.TwoAM)) return "TwoAM";
            else if(operatingHours.equals(OperatingHours.TwoPM)) return "TwoPM";
            else return "OFF";
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.OperatingHours");
        }
    }
}